import { Component } from '@angular/core';
import { NavController, NavParams, ModalController, ItemSliding } from 'ionic-angular';
import { Actives } from './actives';
import { ActiveDataProvider } from '../../providers/active-data/active-data';
import { InputActivesPage } from '../input-actives/input-actives';




@Component({
  selector: 'page-active',
  templateUrl: 'active.html',
})
export class ActivePage {


  actives: Actives[]=[];
  open = new Array();
  done = new Array();
  flag:boolean;

  constructor( public navCtrl: NavController,
    public modalCtrl: ModalController,
    public activeDate:ActiveDataProvider
  ) {
  }


  ionViewDidLoad(){
   
    this.activeDate.getActives().subscribe(actives =>{ 
      this.actives = actives;
      // console.log(this.actives.status);
    });
    let leval=localStorage.getItem('leval');
    if(leval=='0'){
      this.flag=false;
    }else{
      this.flag=true;
    }
  
    }

  addActives(){
    // let theNewActives: string = prompt("新任务");
    // if (theNewActives !== '') {
    //   this.actives.push({title: theNewActives, status: 'open'});
    // }
    let addModal = this.modalCtrl.create(InputActivesPage);

    addModal.onDidDismiss((actives : Actives) =>{
      if (actives) {
        this.actives.push(actives);
      }
    });

    addModal.present();
   
  }

  // 标记
  markAsDone(slidingItem: ItemSliding, actives: Actives){
    actives.status ='done';
    this.activeDate.updateActives(actives).subscribe();
    slidingItem.close();
  }
  // 删除
  removeActives(slidingItem: ItemSliding, actives: Actives){
    actives.status ='removed';
    let index = this.actives.indexOf(actives);
    if (index > -1) {
      this.actives.splice(index,1);
    }
    this.activeDate.deleteTask(actives).subscribe();
    slidingItem.close();
  }

  /**
   * 下拉刷新
   * @param refresher 
   */
  doRefresh(refresher: any){
    setTimeout(() => {
      this.activeDate.getActives().subscribe(Actives =>{ 
        this.actives = Actives;
      });
      refresher.complete();
    },2000);
  }


}
